package com.iwhalecloud.citybrain.flow.platform.manager.application.config;

import com.aliyun.gts.enterprise.foura.auto.FouraAutoConfigurationProperties;
import com.iwhalecloud.citybrain.flow.platform.manager.application.common.filter.EmbedRequestFilter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author lilh
 * @date 2021/4/7 12:25
 */
@Configuration
@ConditionalOnProperty(
        prefix = "gts.enterprise.foura",
        name = {"enable"},
        havingValue = "true"
)
public class EmbedFilterConfig {

    @Autowired
    private FouraAutoConfigurationProperties configurationProperties;

    @Bean
    public FilterRegistrationBean<EmbedRequestFilter> embedRequestFilter() {
        FilterRegistrationBean<EmbedRequestFilter> filterRegistrationBean = new FilterRegistrationBean<>(new EmbedRequestFilter());
        filterRegistrationBean.setOrder(4);
        filterRegistrationBean.addUrlPatterns("/*");
        filterRegistrationBean.setName("embedRequestFilter");
        filterRegistrationBean.addInitParameter("loginUrl", this.configurationProperties.getCasUrl() + "/cas/login?service=" + this.configurationProperties.getServerUrl() + this.configurationProperties.getTicketUrl());
        return filterRegistrationBean;
    }
}
